#pragma once
#include "errorInfo.h"

namespace DSP
{
    template<class T>
    class DSplayer;
    class simpleGraph;
	class Ivideo;
	class errorInfo;
}

class CPlayControl
{
public:
	DSP::DSplayer<DSP::simpleGraph> * m_DSplayer;
	DSP::Ivideo* m_Ivideo;

	int m_Pos;
	int m_Status;
	int m_CurVolume;

	double m_AspectRatio;

	enum PlayStatus {NO_FILE, FILE_PLAY, FILE_PAUSE, FILE_STOP};
	
	bool m_IsVolume;
	bool m_IsVideo;

	CString m_FileName;

	CPlayControl(void): 
		m_DSplayer(NULL),
		m_Ivideo(NULL),
		m_Pos(0),
		m_Status(NO_FILE),
		m_AspectRatio(0),
		m_CurVolume(100),
		m_IsVolume(true),
		m_IsVideo(false)
	{
	}

	~CPlayControl(void)
	{
		if(m_DSplayer!=NULL)
			delete m_DSplayer;
		if(m_Ivideo!=NULL)
			delete m_Ivideo;
	}

	void SetFileName(CString FileName)
	{
		if(m_Status!=NO_FILE)
		{
			delete m_DSplayer;
			delete m_Ivideo;
		}
		m_DSplayer=new DSP::DSplayer<DSP::simpleGraph>(new DSP::simpleGraph(m_FileName));
		if(m_DSplayer->getVideo())
		{
			m_Ivideo=m_DSplayer->getVideo();
			m_AspectRatio=m_Ivideo->getAspectRatio();
			m_IsVideo=true;
		}
		else
			m_IsVideo=false;

		m_Status=FILE_STOP;
		m_Pos=0;
	}

	int GetDuration(void)
	{
		if(m_Status!=NO_FILE)
		{
			LONGLONG m_Duration;
			try
			{
				m_DSplayer->getDuration(m_Duration);
				return m_Duration;
			}
			catch(const DSP::errorInfo& erInfo)
			{
				CString m_ErrStr;
				m_ErrStr.Format(_T("%s"),erInfo.what());
				AfxMessageBox(m_ErrStr);
			}
		}
		return 0;
	}

	bool StartPlaying(void)
	{
		try
		{
			if(m_IsVolume)
				m_DSplayer->setVolume(m_CurVolume);
			else
				m_DSplayer->setVolume(0);				

			m_DSplayer->seek(m_Pos);
			m_DSplayer->play();
			m_Status=FILE_PLAY;
			return true;
		}
		catch(const DSP::errorInfo& erInfo)
		{
			CString m_ErrStr;
			m_ErrStr.Format(_T("%s"),erInfo.what());
			AfxMessageBox(m_ErrStr);
		}
		return false;
	}

	bool StopPlaying(void)
	{
		if(m_Status!=NO_FILE && m_Status!=FILE_STOP)
		{
			try
			{
				m_DSplayer->stop();
				m_Status=FILE_STOP;
				m_Pos=0;
				return true;
			}
			catch(const DSP::errorInfo& erInfo)
			{
				CString m_ErrStr;
				m_ErrStr.Format(_T("%s"),erInfo.what());
				AfxMessageBox(m_ErrStr);
			}
		}
		return false;
	}

	bool PausePlaying(void)
	{
		try
		{
			if(m_Status==FILE_PLAY)
			{
				m_DSplayer->pause();
				m_Status==FILE_PAUSE;
				return true;
			}
		}
		catch(const DSP::errorInfo& erInfo)
		{
			CString m_ErrStr;
			m_ErrStr.Format(_T("%s"),erInfo.what());
			AfxMessageBox(m_ErrStr);
		}
		return false;
	}

	bool RenewPlaying(void)
	{
		try
		{
			if(m_Status==FILE_PAUSE)
			{
				m_DSplayer->play();
				m_Status==FILE_PLAY;
				return true;
			}
		}
		catch(const DSP::errorInfo& erInfo)
		{
			CString m_ErrStr;
			m_ErrStr.Format(_T("%s"),erInfo.what());
			AfxMessageBox(m_ErrStr);
		}
		return false;
	}

	int GetPos(void)
	{
		return m_Pos;
	}
	
	double GetAspectRatio(void)
	{
		return m_AspectRatio;
	}

	bool isVolume(void)
	{
		return m_IsVolume;
	}

	bool isVideo(void)
	{
		return m_IsVideo;
	}

	void SetIsVolume(bool IsVolume)
	{
		m_IsVolume=IsVolume;
		if(IsVolume && m_Status!=NO_FILE)
			m_DSplayer->setVolume(m_CurVolume);
		if(!IsVolume && m_Status!=NO_FILE)
			m_DSplayer->setVolume(0);
	}

	int GetStatus(void)
	{
		return m_Status;
	}

	bool SetWindowPos(long left, long top, long width, long heigth)
	{
		if(m_Ivideo!=NULL)
		{
			try
			{
				m_Ivideo->setWindowPosition(left, top, width, heigth);
			}
			catch(const DSP::errorInfo& erInfo)
			{
				CString m_ErrStr;
				m_ErrStr.Format(_T("%s"),erInfo.what());
				AfxMessageBox(m_ErrStr);
			}
		}
	}
};

